package com.haobangs.imms.task;

import cn.hutool.core.date.DateUtil;
import com.haobangs.imms.domain.Factory;
import com.haobangs.imms.service.IFactoryService;
import com.haobangs.imms.smsUtils.AliyunSmsService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.constant.Constants;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author HaoBangs
 * @Date 2023年 07月 26日 20: 28
 * @Description 定时任务
 **/
@Slf4j
@Component
public class ImmsTask {
    @Autowired
    private AliyunSmsService aliyunSmsService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IFactoryService factoryService;
    @Autowired
    private RedisCache redisCache;

    @Scheduled(cron = "0 00 8 * * ?")
    public void smsTimer(){
//        新的定时任务
//        读取redis缓存数据
        long workshopId = 1;

// 抽屑系统
        String deviceSerialNumber_chou = "imms_75";
// PLC
        String deviceSerialNumber_plc = "imms_76";

        String redisDeviceStatus_chou = redisCache.getCacheMapValue(Constants.DEVICE_STATUS + workshopId, deviceSerialNumber_chou);
        String redisDeviceStatus_plc = redisCache.getCacheMapValue(Constants.DEVICE_STATUS + workshopId, deviceSerialNumber_plc);

        List<String> phoneList = userService.getAllPhoneNumbers();
        long currentTimeMillis = System.currentTimeMillis();
        // 将时间戳转换为Date类型
        Date date = new Date(currentTimeMillis);
        String edgeTime =DateUtil.formatDateTime(date);
        List<Factory> factories = factoryService.list();
        String factoryName = factories.get(0).getFactoryName();
        for (String phone:phoneList
             ) {
            if (StringUtils.isNotEmpty(phone)){
                log.info("执行定时任务");
//                aliyunSmsService.sendCodeAsync(phone,factoryName,"IMMS系统正常运行", edgeTime);
                if (redisDeviceStatus_chou != null && !redisDeviceStatus_chou.isEmpty()) {
                    // 字符串不为空
                    if (redisDeviceStatus_chou.equals("1")){
                        aliyunSmsService.sendCodeAsync(phone,factoryName,"抽屑系统设备正常运行", edgeTime);
                    } else {
                        // 字符串为空
                        log.info("抽屑系统运行数据为0");
                        aliyunSmsService.sendCodeAsync(phone,factoryName,"抽屑系统设备未自动运行", edgeTime);

                    }
                } else {
                    // 字符串为空
                    log.info("抽屑系统运行数据为空");
                    aliyunSmsService.sendCodeAsync(phone,factoryName,"抽屑系统设备未自动运行", edgeTime);

                }

//                if (redisDeviceStatus_plc != null && !redisDeviceStatus_plc.isEmpty()) {
//                    if (redisDeviceStatus_plc.equals("1")) {
//                        aliyunSmsService.sendCodeAsync(phone, factoryName, "主PLC在线", edgeTime);
//                    }
//                } else {
//                    // 字符串为空
//                    aliyunSmsService.sendCodeAsync(phone, factoryName, "主PLC掉线", edgeTime);
//
//                    log.info("主PLC运行数据为空");
//                }

            }

        }
    }

}
